<template>
  <div id="mySpacePage">
    <p>正在跳转,请稍后</p>
  </div>
</template>
<script lang="ts" setup>
import { userLoginStore } from '@/stores/userLoginStore'
import { useRouter } from 'vue-router'
import { listSpaceVoByPageUsingPost } from '@/api/spaceController'
import { message } from 'ant-design-vue'
import { onMounted } from 'vue'
const router = useRouter()
const loginUserStore = userLoginStore()
// 检查用户是否有个人空间
const checkUserSpace = async () => {
  const loginUser = loginUserStore.loginUser
  if (!loginUser?.id) {
    router.replace('user/login')
    return
  }
  const res = await listSpaceVoByPageUsingPost({
    userId: loginUser.id,
    current: 1,
    pageSize: 1,
    spaceType: 0,
  })
  if (res.data.code == 200) {
    if (res.data.data?.records?.length > 0) {
      const space = res.data.data.records[0]
      router.replace(`space/${space.id}`)
    } else {
      router.replace('add_space')
      message.warn('没有个人空间，请创建一个')
    }
  } else {
    message.error('获取空间异常:' + res.data.message)
  }
}
onMounted(() => {
  checkUserSpace()
})
</script>
<style scoped>
#mySpacePage {
  max-width: 720px;
  margin: 0 auto;
}
</style>